<template>
  <div>
    <div class="bg_fff p_10 listbody">
      <div class="flexrowbetween flexwrapper" style="align-items: flex-start">
        <div class="flex">
          <div
            class="iconbox flexcenter commhover mr_10"
            @click="resetQuery"
          >
            <i class="el-icon-refresh f-20" :class="{ rotate: isrefresh }"></i>
          </div>
          <el-button plain type="primary" size="mini" @click="handleAdd">模块添加</el-button>
          <el-button plain type="primary" size="mini" @click="handleAdver">开屏广告</el-button>
          <el-button plain type="primary" size="mini" @click="handleOpenhome">上首页</el-button>
          <el-button plain type="primary" size="mini" @click="handleOpenSign">签 到</el-button>
        </div>
        <div>
          <el-form
            :model="queryParams"
            ref="queryForm"
            size="small"
            :inline="true"
            label-width="0"
          >
            <el-form-item prop="keyword">
              <el-input
                class="searchinput"
                v-model="queryParams.keyword"
                placeholder="请输入活动名称"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <!-- <el-form-item>
              <el-dropdown trigger="click" @command="handleCommand">
                <el-button size="mini" class="nol_border plr_9">
                  <svg-icon class-name="f-14" icon-class="export" />
                  <i class="el-icon-caret-bottom el-icon--right"></i>
                </el-button>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item
                    v-for="(item, index) in exportlist"
                    :key="index"
                    :command="item.id"
                    >{{ item.name }}</el-dropdown-item
                  >
                </el-dropdown-menu>
              </el-dropdown>
            </el-form-item> -->
            <el-form-item class="">
              <el-dropdown trigger="click">
                <el-button size="mini" class="plr_9">
                  <svg-icon class-name="f-14" icon-class="listicon" />
                  <i class="el-icon-caret-bottom el-icon--right"></i>
                </el-button>
                <el-dropdown-menu slot="dropdown">
                  <div class="dropbody">
                    <el-checkbox-group
                      v-model="checkedOption"
                      class="flexcolumn"
                      @change="changeSelect"
                    >
                      <el-checkbox
                        v-for="(item, index) in selectoptions"
                        :label="item.label"
                        :key="index"
                        class="m_5"
                      >
                      {{ item.label }}
                      </el-checkbox>
                    </el-checkbox-group>
                  </div>
                </el-dropdown-menu>
              </el-dropdown>
            </el-form-item>
            <!-- <el-form-item>
              <el-button
                size="mini"
                class="nol_border"
                :loading="exportloading"
                @click="download"
              >
                <svg-icon class-name="f-14" icon-class="export" />
              </el-button>
            </el-form-item> -->

            <el-form-item>
              <el-button class="f-14"
                icon="el-icon-search"
                size="mini"
                @click="handleQuery"
              ></el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
      <el-table
        v-loading="loading"
        :data="tableData"
        border
        ref="tablelayout"
        class="commtable"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column align="center"
          label="排序"
          prop="id"
          width="80"
          v-if="homeactiveOption.includes('排序')"
        >
          <template slot-scope="scope">
            <span>{{scope.row.level}}</span>
            <!-- <span>{{(queryParams.page - 1) * queryParams.num + scope.$index + 1}}</span> -->
          </template>
        </el-table-column>
        <el-table-column align="center"
          label="ID"
          prop="id"
          width="80"
          v-if="homeactiveOption.includes('ID')"
        ></el-table-column>
        <el-table-column v-if="homeactiveOption.includes('模块类型')" align="center"
          label="模块类型"
          prop="name"
        >
          <template slot-scope="scope">
            <span v-if="scope.row.module">{{scope.row.module.type_name}}</span>
            <span v-else>-</span>
          </template>
        </el-table-column>
        <el-table-column align="center"
          label="活动编号"
          prop="t_id"
          v-if="homeactiveOption.includes('活动编号')"
        ></el-table-column>
        <el-table-column align="center"
          label="活动名称"
          prop="level"
          width="300"
          v-if="homeactiveOption.includes('活动名称')"
        >
          <template slot-scope="scope">
            <span v-if="scope.row.task">{{scope.row.task.name}}</span>
            <span v-else>-</span>
          </template>
        </el-table-column>
        <el-table-column align="center"
          label="助力方式"
          prop="level"
          v-if="homeactiveOption.includes('助力方式')"
        >
          <template slot-scope="scope">
            <span v-if="scope.row.taskhelp">
              <span v-if="scope.row.taskhelp.type==1">关注公众号</span>
              <span v-else-if="scope.row.taskhelp.type==2">加好友</span>
              <span v-else-if="scope.row.taskhelp.type==3">观看视频</span>
              <span v-else-if="scope.row.taskhelp.type==4">邀请好友</span>
              <span v-else>-</span>
            </span>
            <span v-else>-</span>
          </template>
        </el-table-column>
        <el-table-column align="center"
          label="活动时间"
          v-if="homeactiveOption.includes('活动时间')"
          prop="level"
          width="300"
        >
          <template slot-scope="scope">
            <span v-if="scope.row.task">
              {{scope.row.task.draw_startime}} - {{scope.row.task.draw_endtime}}
            </span>
            <span v-else>-</span>
          </template>
        </el-table-column>
        <el-table-column align="center"
          label="活动状态"
          v-if="homeactiveOption.includes('活动状态')"
          prop="level"
        >
          <template slot-scope="scope">
            <span v-if="!scope.row.task">-</span>
            <div v-else>
              <template v-if="scope.row.task.status == 1">
                <el-tag 
                  v-if="new Date(scope.row.task.draw_startime).getTime() > Date.now()"
                  type="warning"
                  class="tag_success"
                >未开始</el-tag>
                <el-tag v-else type="success" class="tag_success">进行中</el-tag>
              </template>
              <el-tag v-if="scope.row.task.status == 4" type="danger" class="tag_warning">已结束</el-tag>
              <el-tag v-if="scope.row.task.status == 0" type="info" class="tag_info">未发布</el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column align="center"
          label="开奖状态"
          v-if="homeactiveOption.includes('开奖状态')"
          prop="level"
        >
          <template slot-scope="scope">
            <span v-if="scope.row.task">
              <span v-if="scope.row.task.type == 1">即抽开奖</span>
              <span v-else-if="scope.row.task.type == 2">定时开奖</span>
              <span v-else-if="scope.row.task.type == 3">满人开奖</span>
            </span>
            <span v-else>-</span>
          </template>
        </el-table-column>
        <el-table-column label="是否上首页"
          align="center"
          v-if="homeactiveOption.includes('是否上首页')"
          width="120"
        >
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.status"
              active-color="#13ce66"
              inactive-color="#eee"
              :inactive-value="0"
              :active-value="1"
              @change="changeStatus($event,scope.row)"
            />
          </template>
        </el-table-column>
        <el-table-column align="center"
          v-if="homeactiveOption.includes('创建时间')"
          label="创建时间"
          prop="level"
          width="180"
        >
          <template slot-scope="scope">
            <span v-if="scope.row.task">{{scope.row.task.create_time}}</span>
            <span v-else>-</span>
          </template>
        </el-table-column>
        <el-table-column align="center"
          v-if="homeactiveOption.includes('创建人')"
          label="创建人"
          prop="level"
          width="180"
        >
          <template slot-scope="scope">
            <span v-if="scope.row.task">
              <span v-if="scope.row.task.admin">{{scope.row.task.admin.user_login||'-'}}</span>
              <span v-else-if="scope.row.task.member">{{scope.row.task.member.user_login||'-'}}</span>
              <span v-else>-</span>
            </span>
            <span v-else>-</span>
          </template>
        </el-table-column>
        <el-table-column
            prop="created_at"
            label="操作"
            width="300"
            fixed="right"
            align="center"
        >
          <template slot-scope="scope">
            <div class="flex btnflex">
                <div class="commhover btn_green c_fff mr_10"
                    @click="handleSort(scope.row)">
                    <i class="el-icon-sort f-14"></i>排序
                </div>
                <div class="commhover btn_green c_fff mr_10"
                    @click="handleUpdate(scope.row)">
                    <i class="el-icon-edit f-14"></i> 编辑
                </div>
                <div class="commhover btn_yellow c_fff mr_10"
                    @click="handleDelete(scope.row)">
                    <i class="el-icon-delete f-14"></i> 删除
                </div>
                <!-- v-if="scope.row.task.type == 2||scope.row.task.type == 3" -->
                <div v-if="scope.row.task.type == 1" class="commhover btn_blue mr_10 c_fff"
                  @click="handleOpenDraw(scope.row)"
                >
                  指定开奖
                </div>
                <div v-if="scope.row.task.type == 2||scope.row.task.type == 3" class="commhover btn_blue mr_10 c_fff"
                  @click="handleOpenTime(scope.row)"
                >
                  定时开奖
                </div>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.page"
        :limit.sync="queryParams.num"
        @pagination="getList"
      />
    </div>
    <!-- <el-dialog :title="title"
      :visible.sync="open"
      width="580px"
      class="dialogheaderbg"
      :before-close="handleCloseAdd">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="分类名称" prop="name" v-if="type !== 3">
          <el-input v-model="form.name" placeholder="请输入分类名称" />
        </el-form-item>
        <el-form-item label="排序值" prop="level" v-if="type !== 2 ">
          <el-input v-model="form.level" placeholder="排序值（0-10000）" type="number" />
        </el-form-item>
        <el-form-item label="状态" v-if="type !== 3">
          <el-radio-group v-model="form.status">
            <el-radio :label="1">正常</el-radio>
            <el-radio :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="handleCloseAdd">取 消</el-button>
      </div>
    </el-dialog> -->
    <el-dialog title="修改排序"
      :visible.sync="levelopen"
      width="580px"
      class="dialogheaderbg"
      :close-on-click-modal='false'
      :before-close="handleCloseLevel">
      <el-form ref="levelform" :model="levelform" :rules="levelrules" label-width="100px">
        <el-form-item label="排序值" prop="level">
          <el-input v-model="levelform.level" placeholder="排序值（0-10000）" type="number" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitLevel">确 定</el-button>
        <el-button @click="handleCloseLevel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="title"
      :visible.sync="editopen"
      :close-on-click-modal='false'
      width="620px"
      class="dialogheaderbg"
      :before-close="handleCloseEdit">
      <ModelEdit v-if="editopen" :id='editId' :is_round='is_round' @close='handleCloseEdit' />
    </el-dialog>

    <el-dialog title="模块添加"
      :visible.sync="addopen"
      width="80%"
      class="dialogheaderbg"
      :before-close="handleCloseAdd"
      :close-on-click-modal='false'
    >
      <ModelClass v-if="addopen" />
    </el-dialog>

    <el-dialog title="开屏广告"
      :visible.sync="adveropen"
      width="1000px"
      class="dialogheaderbg"
      :before-close="handleCloseAdver"
      :close-on-click-modal='false'>
      <adver-open v-if="adveropen" />
    </el-dialog>

    <!-- <el-dialog title="上首页"
      :visible.sync="homeopen"
      width="800px"
      class="dialogheaderbg"
      :before-close="handleClosehome">
      <home-up v-if="homeopen" />
    </el-dialog> -->

    <el-dialog title="签到"
      :visible.sync="signopen"
      width="800px"
      class="dialogheaderbg"
      :before-close="handleClosesign"
      :close-on-click-modal='false'>
      <sign-in v-if="signopen" @close='handleClosesign' />
    </el-dialog>

    <el-dialog
      class="dialogheaderbg"
      title="定时开奖"
      :visible.sync="timeopen"
      width="80%"
      :before-close="handleCloseTime"
      :close-on-click-modal='false'
    >
      <time-comp v-if="timeopen" :row="row" :type='2' @close="handleCloseTime" />
    </el-dialog>

    <el-dialog
      class="dialogheaderbg"
      title="指定开奖"
      :visible.sync="drawopen"
      width="80%"
      :before-close="handleCloseDraw"
      :close-on-click-modal='false'
    >
      <draw-comp v-if="drawopen" :row="row" :type='2' @close="handleCloseDraw" />
    </el-dialog>
  </div>
</template>

<script>
import { mapState } from "vuex";
import ModelClass from './components/ModelClass.vue'
import AdverOpen from './components/AdverOpen.vue'
import HomeUp from './components/HomeUp.vue'
import SignIn from './components/SignIn.vue'
import ModelEdit from './components/ModelEdit.vue'
import TimeComp from '@/views/active/components/TimeComp.vue'
import DrawComp from '@/views/active/components/DrawComp.vue'
import { taskIndex, moduleTaskDel, moduleTaskStatusOpen, moduleTaskStatusClose, moduleTaskLevel } from '@/api/sethome'
import { getClassIndex, classAdd, classEdit, classLevelEdit, classStatusEdit, classDel} from '@/api/template'
export default {
  data() {
    return {
      queryParams: {
        page: 1,
        num: 10,
        keyword: undefined,
      },
      total: 0,
      loading: false,
      tableData: [],
      isrefresh: false,
      multiple: true,
      // 是否显示弹出层
      addopen: false,
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "分类名称不能为空", trigger: "blur" }
        ],
        level: [
          { required: true, message: "排序值不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "change" }
        ]
      },
      // 开屏广告
      adveropen:false,
      homeopen:false,//上首页
      signopen:false,//签到
      levelopen:false,
      levelform:{
        id:'',
        level:''
      },
      levelrules:{
        level: [{ required: true, message: "排序值不能为空", trigger: "blur" }],
      },
      title:'',
      editopen:false,
      editId:'',
      is_round:'',
      timeopen:false,
      row:'',
      checkedOption: [],
      selectoptions: [
        { id: 1, label: "排序" },
        { id: 2, label: "ID" },
        { id: 3, label: "模块类型" },
        { id: 4, label: "活动编号" },
        { id: 5, label: "活动名称" },
        { id: 6, label: "助力方式" },
        { id: 7, label: "活动时间" },
        { id: 8, label: "活动状态" },
        { id: 9, label: "开奖状态" },
        { id: 13, label: '是否上首页' },
        { id: 10, label: "创建时间" },
        { id: 11, label: "创建人" }
      ],
      exportloading:false,
      drawopen:false
    };
  },

  created() {
    let activeOption = JSON.parse(localStorage.getItem("homeactiveOption"));
    if (activeOption) {
      this.changeSelect(activeOption);
    }
    this.checkedOption = this.homeactiveOption;
    this.getList()
  },

  props:{
    type:{
      default:''
    }
  },

  components:{
    ModelClass,
    AdverOpen,
    HomeUp,
    SignIn,
    ModelEdit,
    TimeComp,
    DrawComp
  },
  computed: {
    ...mapState({
      homeactiveOption: (state) => state.paramsoption.homeactiveOption,
    }),
  },
  methods: {
    getList() {
      this.loading = true;
      let params = {
        page: this.queryParams.page,
        num: this.queryParams.num
      }
      if(this.type){
        params.id = this.type
      }
      if(this.queryParams.keyword){
        params.keyword = this.queryParams.keyword
      }
      taskIndex(params).then((response) => {
        this.tableData = response.data.list;
        this.total = response.data.count;
        this.loading = false;
        this.isrefresh = false;
      });
    },

    changeSelect(arr) {
      this.$store.commit("SET_HOMEACTIVEOPTION", arr);
      this.$nextTick(() => {
        //在数据加载完，重新渲染表格
        this.$refs.tablelayout && this.$refs.tablelayout.doLayout();
      });
    },

    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },

    resetQuery() {
      this.isrefresh = true;
      this.resetForm("queryForm");
      this.handleQuery();
    },

    handleSelectionChange(selection) {
      this.rowids = selection.map((item) => item.id);
      this.multiple = !selection.length;
    },

    changeStatus(val,row){
      console.log('val:',val)
      if(val){
        this.$modal.confirm('是否确认将编号为"' + row.id + '"的数据项上架？').then(function() {
          return moduleTaskStatusOpen({
            id:row.id
          });
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("模块活动上架成功");
        }).catch(() => {
          row.status = val ? 0 : 1
        });
      }else{
        this.$modal.confirm('是否确认将编号为"' + row.id + '"的数据项下架？').then(function() {
          return moduleTaskStatusClose({
            id:row.id
          });
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("模块活动下架成功");
        }).catch(() => {
          row.status = val ? 0 : 1
        });
      }
      // if(val)
     
    },

    reset(){
      this.form = {
        id: undefined,
        name: undefined,
        level:undefined,
        status: 1,
      };
      this.resetForm("form");
    },

    /** 添加模块操作 */
    handleAdd() {
      this.addopen = true;
    },

    handleCloseAdd(){
      this.addopen = false
    },

    
    handleCommand(command) {
      console.log(command);
    },


    /** 修改按钮操作 */
    handleUpdate(row) {
      // this.reset();
      // this.type = 2;
      // this.form.id = row.id;
      // this.form.name = row.name
      // this.form.status = row.status
      this.title = '编辑'
      this.editId = row.id
      this.is_round = row.module.is_round
      this.editopen = true;
    },

    handleCloseEdit(type){
      if(type == 1){
        this.getList()
      }
      this.editopen = false
    },

    // 排序
    handleSort(row) {
      this.levelform.id = row.id;
      this.levelform.level = row.level
      this.levelopen = true;
    },

    handleCloseLevel(){
      this.levelopen = false;
    },

    submitLevel(){
      this.$refs["levelform"].validate(valid => {
        if(!valid) return
        moduleTaskLevel(this.levelform).then(res=>{
          this.$modal.msgSuccess("修改成功");
          this.handleCloseLevel()
          this.getList()
        })
      })
    },

    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.type == 2) {
            classEdit(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else if (this.type == 1)  {
            classAdd(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }else if (this.type == 3){
            classLevelEdit(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const infoIds = row.id || this.rowids;
      this.$modal.confirm('是否确认删除编号为"' + infoIds + '"的数据项？').then(function() {
        return moduleTaskDel({id:infoIds});
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },

    handleAdver(){
      this.adveropen = true
    },

    handleCloseAdver(){
      this.adveropen = false
    },

    handleOpenhome(){
      this.title='上首页'
      this.editId = ''
      this.editopen = true
    },
    
    handleClosehome(){
      this.homeopen = false
    },

    handleOpenSign(){
      this.signopen = true
    },

    handleClosesign(){
      this.signopen = false
    },

    handleOpenTime(row) {
      this.row = row;
      this.row.t_name = row.task.name
      this.timeopen = true;
    },

    handleCloseTime() {
      this.timeopen = false;
    },

    handleOpenDraw(row){
      this.row = row
      this.row.t_name = row.task.name
      this.drawopen = true
    },

    handleCloseDraw(){
      this.drawopen = false
    },


    download() {

    }
  },
};
</script>

<style lang="scss" scoped>
.iconbox {
  width: 32px;
  height: 32px;
  border: 1px solid #eee;
  border-radius: 2px;
}
</style>
